package com.housesales.web;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpSession;

import module.CusLog;
import module.Employees;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import bussinessService.basicbussinessService;
import bussinessService.employeesBussinessService;
import bussinessService.impl.CusLogBussinessServiceImpl;
import bussinessService.impl.EmployeesBussinessServiceImpl;
import domain.vo.CustomerLogQueryObject;
import domain.vo.CustomerLogVO;
import domain.vo.JSONObject;
import domain.vo.Message;

/**
 * 
* @ClassName: CustomersLogController
* @Description: 客户追踪ACTION
* @author chenchao
* @date 2013-12-11 上午05:53:58
*
 */
@Controller
@RequestMapping("/customersLog")
public class CustomersLogController {

	private Logger logger = Logger.getLogger(CustomersLogController.class);
	/**
	 * 客户追踪业务层实例
	 */
	private basicbussinessService<CusLog,CustomerLogVO> custlogBIS = new CusLogBussinessServiceImpl();
	/**
	 * 员工业务层实现类
	 */
	private employeesBussinessService empBIS = new EmployeesBussinessServiceImpl();
	/**
	 * 员工集合
	 */
	private  Employees emp = null;
	/**
	 * 
	 * @return
	 */
	
	@RequestMapping("/viewList")
	public ModelAndView viewAllList() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("customerLog/customerLog");
		return mv;
	}
	
	@RequestMapping("/saveCustomersLog")
	public @ResponseBody Message saveCustomersLog(@RequestParam("cus_id") int cusId,@RequestParam("logcontent") String logContent,HttpSession httpSession){
		Message ms = new Message();
		String e_sn = httpSession.getAttribute("e_sn").toString();
		CusLog cusLog = new CusLog();
		cusLog.setContent(logContent);
		cusLog.setCus_id(cusId);
		cusLog.setE_sn(e_sn);

		try {
			emp = empBIS.getById(e_sn);
			cusLog.setE_name(emp.getE_name());
			custlogBIS.merageObject(cusLog);
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			ms.setErrorMsg("客户追踪保存失败");
		}
		return ms;
	}
	
	@RequestMapping("/getCustomerLogJson") 
	public @ResponseBody JSONObject<CustomerLogVO> getCustomerLogJson(@RequestParam("cus_id") int cus_id) {
		JSONObject<CustomerLogVO> jsonObject = new JSONObject<CustomerLogVO>();
		try {
			CustomerLogQueryObject custQO  = new CustomerLogQueryObject();
			custQO.setCusId(cus_id);
			List<CustomerLogVO> vos = custlogBIS.getObjectByQueryObject(custQO);
			jsonObject.setRows(vos);
			jsonObject.setTotal(vos.size());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return jsonObject;
	}
}
